<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" maxlength="11" placeholder="手机号"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="密码"></wInput>
			</view>
			<wButton text="登 录" :rotate="isRotate" @click.native="startLogin()" class="wbutton"></wButton>



			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
				<text>|</text>
				<navigator url="login2" open-type="navigate">验证码登录</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">用户登录</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '',
				phoneData: '', //用户/电话
				passData: '', //密码
				isRotate: false, //是否加载旋转
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		methods: {
			isShowAgree() {
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			startLogin() {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length != 11) {
					this.$toast.error('请输入正确的手机号')
					return;
				}
				if (this.passData.length <3) {
					this.$toast.error('密码为3~11位')
					return;
				}

			

				uni.request({
					url: "http://localhost:10998/user/login/" + this.phoneData + "/" + this.passData,
					success: (res) => {
						if (res.data.code == 200) {
							console.log(res)
							this.$store.commit('login', res.header['Set-Cookie'])
							let userdata = 'login'
							this.$store.dispatch("setUserData", userdata); //存入状态
							try {
								uni.setStorageSync('setUserData', userdata); //存入缓存
							} catch (e) {
								// error
							}
							this.text = 'request success';
							console.log("登陆成功")
							uni.navigateBack()
						} else
							this.$toast.error(res.data.msg)
					}
				})
				_this.isRotate = true
				setTimeout(function() {
					_this.isRotate = false
				}, 3000)
			},
		},
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
